package com.example.timetable.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.timetable.R;
import com.example.timetable.dao.Course;

import java.util.List;

public class CourseAdapter extends RecyclerView.Adapter<CourseAdapter.ViewHolder> {
    //课程数据
    List<Course> list ;
    Context context;

    public CourseAdapter(Context context,List<Course> list) {
        this.list = list;
        this.context = context;
    }

    //创建ViewHolder并返回，后续item布局里控件都是从ViewHolder中取出
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //将我们自定义的item布局R.layout.item_one转换为View
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.add_course_item, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Course course = list.get(position);
        //获取drawable文件中的图片
        holder.itemView.setBackground(context.getResources().getDrawable(R.drawable.course_bg,null));
        //禁止输入
        holder.etCourseName.setFocusable(false);
        //设置字体颜色
        holder.etCourseName.setTextColor(Color.WHITE);
        holder.etCourseName.setText(course.getCourseName());
        holder.etTeacherName.setFocusable(false);
        holder.etTeacherName.setTextColor(Color.WHITE);
        holder.etTeacherName.setText(course.getTeacherName());
        holder.etStartWeek.setFocusable(false);
        holder.etStartWeek.setTextColor(Color.WHITE);
        //int类型转string
        holder.etStartWeek.setText(String.valueOf(course.getStartWeek()));
        holder.etEndWeek.setFocusable(false);
        holder.etEndWeek.setTextColor(Color.WHITE);
        holder.etEndWeek.setText(String.valueOf(course.getEndWeek()));
    }

    //获取数据源总的条数
    @Override
    public int getItemCount() {
        return list==null? 0 : list.size();
    }
    /**
     * 自定义的ViewHolder
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        EditText etCourseName;
        EditText etTeacherName;
        EditText etStartWeek;
        EditText etEndWeek;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            //绑定控件
            etCourseName = itemView.findViewById(R.id.etCourseName);
            etTeacherName = itemView.findViewById(R.id.etTeacherName);
            etStartWeek = itemView.findViewById(R.id.etStartWeek);
            etEndWeek = itemView.findViewById(R.id.etEndWeek);
        }
    }
}
